package com.zhxy.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhxy.manager.dao.StudentDao;
import com.zhxy.manager.dto.StudentDto;
import com.zhxy.manager.entity.Student;
import com.zhxy.manager.service.intf.StudentService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao dao;
    @Override
    public R save(Student student) {
        if (dao.add(student)>0){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    @Override
    public PageBean queryPage(int page, int limit) {
        //分页插件分页
        PageHelper.startPage(page, limit);
        //获取分页结果（查询数据库数据）
        PageInfo<Student> PageInfo = new PageInfo<>(dao.selectAll());
        //返回分页的数据
        return PageBean.bean(PageInfo.getTotal(),PageInfo.getList());
    }

    @Override
    public R del(int id) {
        if(id>0){
            if(dao.del(id)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R update(Student student) {
        if(dao.update(student)>0){
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public PageBean queryPageWhere(String name, int sex, int page, int limit) {
        //1.条件处理
        if(StringUtils.hasLength(name)){
            name="%"+name+"%";
        }else{
            name=null;
        }
        //2.分页
        PageHelper.startPage(page, limit);
        //3.获取分页结果
        PageInfo<Student> pageInfo=new PageInfo<>(dao.selectWhere(name, sex));
        //4.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }



    @Override
    public R delBatch(int[] ids) {
        //1.验证参数
        if(ids!=null && ids.length>0){
            //2.执行
            if(dao.delBatch(ids)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public PageBean queryPageGrade(String name, int sex, int page, int limit) {
        //1.条件处理
        if(StringUtils.hasLength(name)){
            name="%"+name+"%";
        }else{
            name=null;
        }
        //2.分页
        PageHelper.startPage(page, limit);
        //3.获取分页结果
        PageInfo<StudentDto> pageInfo=new PageInfo<>(dao.selectGrade(name,sex));
        System.err.println(pageInfo.getList());
        //4.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public List<Student> queryAll() {
        return dao.selectAll();
    }
}

